package com.tuanbiao.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;

import com.hzjq.data.entity.BlastLonLat;
import com.hzjq.data.entity.PmEntity;
import com.tuanbiao.R;
import com.tuanbiao.base.mvp.BasePresenterActivity;
import com.tuanbiao.controllers.LocationController;
import com.tuanbiao.databinding.ActivityLocationBinding;
import com.tuanbiao.dialog.WaitDialog;
import com.tuanbiao.models.LocationModel;
import com.tuanbiao.presenter.LocationPresenter;
import com.tuanbiao.utils.ClickUtil;

/**
 * @author abiao
 * @since 2022/4/7 16:12
 */
public class LocationActivity extends BasePresenterActivity<LocationPresenter, ActivityLocationBinding> implements LocationController.View {
    private WaitDialog.Builder dialog;

    public static void start(Context context) {
        Intent starter = new Intent(context, LocationActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected ActivityLocationBinding getBindingView() {
        return ActivityLocationBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void onResume() {
        super.onResume();
        binding.bmlContent.onResume();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding.bmlContent.onCreate(savedInstanceState);
    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        binding.bmlContent.onSaveInstanceState(outState);
    }

    @Override
    protected void initView() {
        binding.btnCommit.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            mPresenter.saveCurrentLocation();
        });
        binding.ivToolbarBack.setOnClickListener(getBackListener());
    }


    @Override
    public void showLoading() {
        if (null == dialog)
            dialog = new WaitDialog.Builder(getActivity()).setMessage("正在加载....");
        if (!dialog.isShowing()) dialog.show();
    }

    @Override
    public void hideLoading() {
        if (null != dialog && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        binding.bmlContent.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding.bmlContent.onDestroy();
    }

    @Override
    protected LocationPresenter createPresenter() {
        return new LocationPresenter(new LocationModel(), this);
    }

    @Override
    public void showLonLat(BlastLonLat blastLonLat) {
        if (!blastLonLat.isAuthRadius()) {
            binding.tvBreakStatus.setText(R.string.location_out_blasting);
            binding.tvLongitude.setTextColor(getCompatColor(R.color.textDanger));
            binding.tvLatitude.setTextColor(getCompatColor(R.color.textDanger));
            binding.tvBreakStatus.setTextColor(getCompatColor(R.color.textDanger));
        } else {
            binding.tvBreakStatus.setText(R.string.location_in_blasting);
            binding.tvLongitude.setTextColor(getCompatColor(R.color.textSuccess));
            binding.tvLatitude.setTextColor(getCompatColor(R.color.textSuccess));
            binding.tvBreakStatus.setTextColor(getCompatColor(R.color.textSuccess));
        }
        binding.tvLatitude.setText(getString(R.string.main_location_latitude, blastLonLat.getLat()));
        binding.tvLongitude.setText(getString(R.string.main_location_longitude, blastLonLat.getLon()));
        binding.tvLongitude.setText(getString(R.string.location_longitude, blastLonLat.getLon()));
        binding.tvLatitude.setText(getString(R.string.location_latitude, blastLonLat.getLat()));
    }

    @Override
    public void showProjectDetails(PmEntity project) {
        binding.tvProjectName.setText(project.getPmName());
    }
}
